package net.skyaims.sqlmgr.resource;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import lombok.RequiredArgsConstructor;
import net.skyaims.sqlmgr.constant.ConstData;
import net.skyaims.sqlmgr.dao.IdcDao;
import net.skyaims.sqlmgr.dao.NodeDao;
import net.skyaims.sqlmgr.domain.Idc;
import net.skyaims.sqlmgr.domain.NodeTable;
import net.skyaims.sqlmgr.util.UUIDUtil;

@Produces(MediaType.APPLICATION_JSON)
@RequiredArgsConstructor
@Path("/sqlmgr/idc")
public class IdcResource {

	private final IdcDao idcDao;
	private final NodeDao nodeDao;

	@Path("/update/uuid")
	@GET
	public String updateIdc() {

		try {

			List<Integer> idcIds = new ArrayList<Integer>();
			List<Integer> nodeIds = new ArrayList<Integer>();

			List<Idc> idcs = idcDao.getAll();
			List<NodeTable> nodes = nodeDao.getAll();

			for (Idc idc : idcs) {
				idcIds.add(idc.getId());
			}

			for (NodeTable node : nodes) {
				nodeIds.add(node.getNid());
			}

			idcIds.retainAll(nodeIds);

			for (Integer id : idcIds) {
				String uuid = UUIDUtil.random();
				idcDao.updateUuidById(id, uuid);
				nodeDao.updateUuidById(id, uuid);
			}

			return ConstData.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ConstData.FAILD;
		}

	}

	public static void main(String[] args) {

		List<Integer> idcIds;
		List<Integer> nodeIds;

		idcIds = new ArrayList<Integer>(Arrays.asList(1, 2, 4, 5));
		nodeIds = new ArrayList<Integer>(Arrays.asList(1, 3, 5));
		idcIds.retainAll(nodeIds);

		System.out.println(idcIds);
		for (Integer i : idcIds) {
			System.out.println(i);
		}

	}
}
